package com.ta.controller;

import com.ta.pojo.Address;
import com.ta.pojo.Cart;
import com.ta.pojo.vo.CartVO;
import com.ta.service.CartService;
import com.ta.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车请求
     * @param cart
     * @param session
     * @return
     */
    @RequestMapping("add")
    public JsonResult<Void> insert (Cart cart, HttpSession session){
        cartService.insert(cart, getUid(session),getUserName(session));
        return new JsonResult<>(200);
    }

    /**
     * 我的购物车请求
     * @param session
     * @return
     */
    @RequestMapping({"/",""})
    public JsonResult<List<CartVO>> findByUid (HttpSession session){
        List<CartVO> cartVOList = cartService.findByUid(getUid(session));
        return new JsonResult<>(200,cartVOList);
    }

    /**
     * 购物车数量加减请求
     * @param cid
     * @param session
     * @param num
     * @return
     */
    @RequestMapping("modify_num")
    public JsonResult<Void> modifyNum (Integer cid, HttpSession session, Integer num){
        cartService.modifyNum(cid, getUid(session), getUserName(session), num);
        return new JsonResult<>(200);
    }

    /**
     * 商品结算请求
     * @param cid
     * @param session
     * @return
     */
    @RequestMapping("settlement")
    public JsonResult<List<CartVO>> settlement (Integer[] cid, HttpSession session){
        List<CartVO> cartVOList = cartService.selectCid(cid,getUid(session));
        return new JsonResult<>(200,cartVOList);
    }

    @RequestMapping("delete_cid")
    public JsonResult<Void> deleteCid (Integer cid, HttpSession session){
        cartService.deleteCid(cid, getUid(session));
        return new JsonResult<>(200);
    }

    @RequestMapping("delete_cids")
    public JsonResult<Void> deleteCids (Integer[] cid, HttpSession session){
        cartService.deleteCids(cid, getUid(session));
        return new JsonResult<>(200);
    }

}
